package net.zoneland.geological.devicedata.service;


import org.apache.log4j.Logger;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import java.util.UUID;

import net.zoneland.geological.devicedata.entity.Tbl_surface_move_device_data;
import net.zoneland.geological.devicedata.entity.Tbl_surface_move_device_data;
import net.zoneland.greenleaf.common.date.DateOperation;
import net.zoneland.geological.devicedata.dao.Tbl_surface_move_device_dataDao;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * tbl_surface_move_device_data[地表XYZ轴位移数据]表操作Serfice实现类

 * 表   名：TBL_SURFACE_MOVE_DEVICE_DATASERVICEIMPL<br/>
 * 注   释：地表XYZ轴位移数据<br/>
 * 作   者：李义<br/>
 * 日   期：2017-05-31 19:33:42
**/
@Service("tbl_surface_move_device_dataService")
public class Tbl_surface_move_device_dataServiceImpl implements Tbl_surface_move_device_dataServiceI { 

	public static Logger logger = Logger.getLogger(Tbl_surface_move_device_dataServiceImpl.class);

	/**
	 * 自动注入DAO对象
	**/
	@Autowired
	private Tbl_surface_move_device_dataDao tbl_surface_move_device_dataDao;

	/**
	 * 新增或者更新数据方法接口
	**/
	@Override
	public Tbl_surface_move_device_data addOrUpdate( Tbl_surface_move_device_data tbl_surface_move_device_data )  throws Exception{
		if ( tbl_surface_move_device_data == null ) {
			logger.error(Tbl_surface_move_device_data.class.getName() + "对象参数信息为NULL，addOrUpdate方法无法继续！");
			return tbl_surface_move_device_data;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		if( tbl_surface_move_device_data.getId() ==null || "".equals( tbl_surface_move_device_data.getId().trim() ) ){
			map.put( "id", "0000-0000-0000-0000" );
		}else{
			map.put( "id", tbl_surface_move_device_data.getId() );
		}
		List<Tbl_surface_move_device_data> list  = tbl_surface_move_device_dataDao.getList(map);
		if( list != null && !list.isEmpty() ){
			tbl_surface_move_device_dataDao.edit( tbl_surface_move_device_data );
			return tbl_surface_move_device_data;
		}else{
			if( tbl_surface_move_device_data.getId() ==null || tbl_surface_move_device_data.getId().trim().isEmpty() ){
				tbl_surface_move_device_data.setId( UUID.randomUUID() + "" );
		    }
			tbl_surface_move_device_dataDao.add( tbl_surface_move_device_data );
			return tbl_surface_move_device_data;
		}
	}
	/**
	 * 新增数据方法实现
	**/
	@Override
	public boolean add(Tbl_surface_move_device_data tbl_surface_move_device_data) throws Exception{
		tbl_surface_move_device_dataDao.add( tbl_surface_move_device_data );
		return true;
	}
	/**
	 * 更新数据方法实现
	**/
	@Override
	public boolean update(Tbl_surface_move_device_data tbl_surface_move_device_data) throws Exception{
		tbl_surface_move_device_dataDao.edit( tbl_surface_move_device_data );
		return true;
	}
	/**
	 * 查询数据方法实现
	**/
	@Override
	public Tbl_surface_move_device_data get( Map<String, Object> map ) throws Exception{
		return tbl_surface_move_device_dataDao.get( map );
	}
	/**
	 * 查询数据方法实现（列表）
	**/
	@Override
	public List<Tbl_surface_move_device_data> getList(Map<String, Object> map) throws Exception{ 
		return tbl_surface_move_device_dataDao.getList( map );
	}
	
	/**
	 * 查询数据总数方法实现
	**/
	@Override
	public int getListCount(Map<String, Object> map) throws Exception{ 
		return tbl_surface_move_device_dataDao.getListCount( map );
	}
	
	/**
	 * 数据删除方法实现
	**/
	@Override
	public boolean delete(Map<String, Object> map) throws Exception{ 
		tbl_surface_move_device_dataDao.delete( map );
		return true;
	}
	@Override
	public List<String> getDeviceNoList(Map<String, Object> paramMap) {
		return tbl_surface_move_device_dataDao.getDeviceNoList( paramMap );
	}
	
	public void analyseDeviceData() {
		String timeChar = new DateOperation().getNowTimeChar();
		List<String> deviceIds = null;
		Map<String, Object> paramMap = new HashMap<>();
		
		//1、查询数据涉及的所有设备ID
		paramMap.put( "analyse_flag_is_null", true );
		deviceIds = getDeviceNoList( paramMap );
		
		if( deviceIds != null && !deviceIds.isEmpty() ){
			for( String device_id : deviceIds ){
				try {
					//更新分析时间标识
					analyseDate( device_id, timeChar );
				} catch (Exception e) {
					e.printStackTrace();
				}
				try {
					//更新设备最新数据标识
					updateLatestDate( device_id );
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	private void analyseDate(String device_id, String timeChar) throws Exception {
		List<Tbl_surface_move_device_data> dataList = null;
		Map<String, Object> paramMap = new HashMap<>();		
		paramMap.put( "device_id", device_id );
		paramMap.put( "analyse_flag_is_null", true );
		dataList = getList( paramMap );
		if( dataList != null && !dataList.isEmpty() ){
			for( Tbl_surface_move_device_data data : dataList ){
				//这里进行进一步数据分析，比如日期时间，所属地区，等等
				//1、分析数据的area_code, 从device_id查询device获取
				//paramMap.clear();
				//paramMap.put( "no", device_id );
				
				data.setAnalyse_flag( timeChar );
				update( data );
			}
		}
	}

	/**
	 * 更新指定设备所有数据的最新数据
	 * @param device_id
	 * @throws Exception
	 */
	private void updateLatestDate( String device_id ) throws Exception {
		List<Tbl_surface_move_device_data> dataList = null;
		Map<String, Object> paramMap = new HashMap<>();
		
		//查询最新数据，并且将最新数据标识重置为0
		paramMap.put( "device_id", device_id );
		paramMap.put( "islatest", 1 );
		dataList = getList( paramMap );
		if( dataList != null && !dataList.isEmpty() ){
			for( Tbl_surface_move_device_data _data : dataList ){
				//分析并且添加数据的年月日信息
				_data.setIslatest( 0 );
				updateLatestFlag( _data );
			}
		}
		
		paramMap.clear();
		paramMap.put( "device_id", device_id );
		paramMap.put( "orderColName", "observe_time");
		paramMap.put( "orderType", "desc");
		paramMap.put( "startRow", 0);
		paramMap.put( "pageSize", 1);
		dataList = getList( paramMap );
		if( dataList != null ){
			dataList.get(0).setIslatest( 1 );
			updateLatestFlag( dataList.get(0) );
		}
	}
	
	private boolean updateLatestFlag(Tbl_surface_move_device_data _data) {
		tbl_surface_move_device_dataDao.updateLatestFlag( _data );
		return true;
	}
}
